<?php
		include "../common/db.inc";				
		$error = '';
		//xoa
		if (isset($_GET["deletedId"]))
		{
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$mysqli->set_charset('utf8');	
			/* check connection */
			if (mysqli_connect_errno()) {
			    printf("Connect failed: %s\n", mysqli_connect_error());
			    exit();
			$mysqli->close();
			}
			
			$id = $_REQUEST["deletedId"];
			$query = "call sp_GetAllEbookByCategory('$id')";
			$result = $mysqli->query($query);
			$mysqli->close();
			if($result)
			{
				if(mysqli_num_rows($result) != 0)
				{
					$error = "Đã có ebook thuộc danh mục này. Không thể xóa danh mục này";	
				}
				else
				{
					$mysqli = new mysqli($host, $username, $password,$dbname);
					$query = "call sp_DeleteCategory('$id')";
					$mysqli->query($query);
					$mysqli->close();
				}
			}
			
		}
		
		//them
		if (isset($_GET["addedName"]))
		{
			$name = $_GET["addedName"];
			$chosen_field = $_GET["f"];
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$mysqli->set_charset('utf8');	
			/* check connection */
			if (mysqli_connect_errno()) 
			{
			    printf("Connect failed: %s\n", mysqli_connect_error());
			    exit();
				$mysqli->close();
			}
			$query = ("call sp_AddCategory('$name', '$chosen_field')");
			$mysqli->query($query);
			$mysqli->close();
		}
		
		//cap nhat
		if (isset($_GET["updatedId"]))
		{
			$mysqli = new mysqli($host, $username, $password,$dbname);
			$mysqli->set_charset('utf8');	
			/* check connection */
			if (mysqli_connect_errno()) 
			{
			    printf("Connect failed: %s\n", mysqli_connect_error());
			    exit();
				$mysqli->close();
			}
			$id = $_REQUEST["updatedId"];
			$name = $_REQUEST["name"];
			$f = $_REQUEST['fld'];
			$query = ("call sp_UpdateCategory('$id', '$name', $f)");
			$mysqli->query($query);
			$mysqli->close();
		}
		
		$query = "call sp_GetAllCategoryAndFieldName()";
		$chosen_field = '';
		if(isset($_GET['f']))
		{
			$chosen_field = $_GET['f'];
			if($chosen_field != 0)
				$query = "call sp_GetCategoryAndFieldByField('$chosen_field')";
		}
		
		echo "<div id='divResult'>";
		echo "<div style='text-align:center'>Chọn lĩnh vực&nbsp;";
		$arrField = GetField();		
		echo "<select id='field' onchange='showCategory(this)'>";
		echo "<option value='0'>Tất cả</option>";
		foreach($arrField as $fieldkey=>$fieldvalue)
		{
			if($fieldkey == $chosen_field)
				echo "<option selected value='$fieldkey'>$fieldvalue</option>";
			else
				echo "<option value='$fieldkey'>$fieldvalue</option>";
		}
		echo "</select></div><br/>";
		
		$mysqli = new mysqli($host, $username, $password,$dbname);
		$mysqli->set_charset('utf8');	
		/* check connection */
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		
		if ($result = $mysqli->query($query)) 
		{			
			if($error != '')
				echo "<div style='text-align: center; color: red'>$error</div><br/>";
			echo "<table border='1' align = 'center'>";
			echo "<tr style='background-color:#AACCEE' >";
			echo "<th>ID</th>";
			echo "<th>Tên</th>";
			echo "<th>Lĩnh vực</th>";
			echo "<td>&nbsp;</td></tr>";
			
			$i = 0;
    		while ($row = $result->fetch_row()) 
			{
				$id = $row[0];
				$name = $row[1];
				$field = $row[2];
				$i = 1 - $i;
				if($i == 0)
					echo "<tr style='background-color:#FFFFE1'>";
				else 
					echo "<tr style='background-color:#E5E5E5'>";
				echo"<td style='text-align:center'>$id</td>";
				echo"<td>$name</td>";
				echo"<td>$field</td>";
				echo "<td><input type=\"button\" name=\"btnXoa\" value=\"Xóa\" onclick=\"XuLyXoa('$id',2);\"/>";
				echo "<input type=\"button\" name=\"btnCapNhat\" value=\"Cập nhật\" onclick=\"btnCapnhat_Click(this, 2)\" /></td>";
				echo"</tr>";
				
			}
		}
		$mysqli->close();
		echo"<tr>";
		echo "<td width='40px'></td>";
		echo "<td><input type=\"text\" id =\"txtName\" size='30' /></td>";		

		echo "<td><select id='cbbField'>";		
		foreach($arrField as $fieldkey=>$fieldvalue)
		{
			echo "<option value='$fieldkey'>$fieldvalue</option>";
		}
		echo '</select></td>';
		echo "<td><input type=\"button\" name=\"btnThem\" value=\"Thêm\" onclick=\"XuLyThem(2);\"/></td>";
		echo"</tr>";
		echo "</table>";	
		echo "</div>";
?>

<?php
function GetField()
{
	include "../common/db.inc";
	$mysqli = new mysqli($host, $username, $password,$dbname);
	$mysqli->set_charset('utf8');	
	/* check connection */
	if (mysqli_connect_errno()) {
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	}
	$query = ("call sp_GetAllField()");
	if ($result = $mysqli->query($query)) 
	{
		while ($row = $result->fetch_row()) 
		{
			$arrField[$row[0]] = $row[1];
		}
	}
	return $arrField;
}

?>